const { ref } = Vue;
import { useFetch } from '../../Scripts/hooks/useFetch.js'

export function useCommon() {
  const factoryUserattributeList = ref([]);
  const uomList = ref([]);
  const lossReasonList = ref([]);//报废原因
  const unqualifiedFeatureList = ref([]);//不合格品特征

  // 获取分厂下自定义属性
  const getFactoryUserattributes = async configObj => {
    const { data } = await useFetch({
      url: `${configObj.url}?actions=${configObj.method}`,
      data: {
        type: `${configObj.method}`,
        factoryid: configObj.factoryid
      }
    });
    factoryUserattributeList.value = data.value;
  }

  // 获取uom
  const getUomList = async configObj => {
    const { data } = await useFetch({
      url: `${configObj.url}?actions=${configObj.method}`,
      data: { type: `${configObj.method}` }
    });
    uomList.value = data.value;
  }

  // 获取报废原因
  const getLossReasonByGroupName = async configObj => {
    const { data } = await useFetch({
      url: `${configObj.url}?actions=${configObj.method}`,
      data: {
        type: `${configObj.method}`,
        lossreasongroupname: configObj.lossreasongroupname
      }
    });
    lossReasonList.value = data.value;
  }

  // 获取不合格品特征
  const getUnqualifiedFeatureList = async configObj => {
    const { data } = await useFetch({
      url: `${configObj.url}?actions=${configObj.method}`,
      data: {
        type: `${configObj.method}`,
        factoryid: `${configObj.factoryid}`
      }
    });
    unqualifiedFeatureList.value = data.value;
  }

  const generateUUID = () => {
    let d = new Date().getTime(); // 时间戳
    let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      let r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
  }

  // 根据分厂代码查分厂
  const getFactoryListByCode = async configObj => {
    const { data } = await useFetch({
      url: `../../../Ashx/FetchCommonHandler.ashx?actions=getFactoryListByCode`,
      data: { type: `getFactoryListByCode`, ...configObj }
    });
    return data.value;
  }

  return {
    factoryUserattributeList,
    uomList,
    lossReasonList,
    unqualifiedFeatureList,
    getFactoryUserattributes,
    getUomList,
    getLossReasonByGroupName,
    getUnqualifiedFeatureList,
    generateUUID,
    getFactoryListByCode
  }
}